﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.Controls;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;

namespace Basic4Net.OAWeb.Module_Base
{
    public partial class WorkTitleAndTeacher : BasePage
    {
        /// <summary>
        /// 职称关系逻辑层
        /// </summary>
        private static PositionRelation _pr;
        protected static PositionRelation HPostionRelation
        {
            get { return _pr ?? (_pr = new PositionRelation()); }
        }

        /// <summary>
        /// 职称逻辑层
        /// </summary>
        private static Position _postionInstance;
        protected static Position HPositon
        {
            get { return _postionInstance ?? (_postionInstance = new Position()); }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            BindPosition();
            BindTeacher(0, aspNetPager.PageSize);
        }

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        protected void BtnSearchClick(object sender, EventArgs e)
        {
            BindTeacher((aspNetPager.PageIndex-1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }

        /// <summary>
        /// 分页控件事件
        /// </summary>
        protected void AspNetPagerPageChanged(object sender, BNPager.PageChangedEventArgs e)
        {
            //update by lxy 20100916 添加跳转页的页数必须大于0 修改bug34
            if (e.NewPageIndex > 0)
            {
                aspNetPager.PageIndex = e.NewPageIndex;
                BindTeacher((e.NewPageIndex - 1)*aspNetPager.PageSize, aspNetPager.PageSize);
            }
        }

        /// <summary>
        /// 职称分类下拉事件
        /// </summary>
        protected void DrPositionChanged(object sender,EventArgs e)
        {
            BindTeacher(0, aspNetPager.PageSize);
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="startAt">skip数据条数</param>
        /// <param name="step">取数据的条数</param>
        private void BindTeacher(int startAt, int step)
        {
            int count = 0;
            IList<object> resultList = HPostionRelation.GetTeacherByPosition(startAt, step, 
                drPosition.SelectedValue == "0"? (int?) null : Convert.ToInt32(drPosition.SelectedValue),
                txtCode.Text, ref count);
            aspNetPager.RecordCount = count;
            Repeater1.DataSource = resultList;
            Repeater1.DataBind();
        }

        /// <summary>
        /// 职称分类下拉框绑定
        /// </summary>
        private void BindPosition()
        {
            IList<t_Position> list = HPositon.GetAllPosition();

            drPosition.DataSource = list;  
            drPosition.DataTextField ="FName";
            drPosition.DataValueField="RID";
            drPosition.DataBind(); 
            drPosition.Items.Add(new ListItem("--所有分类--", "0"));
            drPosition.SelectedValue = "0";

            drPositionInPop.DataSource = list;
            drPositionInPop.DataTextField = "FName";
            drPositionInPop.DataValueField = "RID";
            drPositionInPop.DataBind(); 
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="teacherIds">教师id列表</param>
        /// <param name="positionId">职称id</param>
        /// <returns></returns>
        [System.Web.Services.WebMethod]
        public static string UpdatePosition(List<int> teacherIds, int positionId)
        {
            string strmsg = String.Empty;
            HPostionRelation.UpdateTeacherPosition(teacherIds, positionId, SchoolMark, ref strmsg);
            return strmsg;
        }
    }
}